package com.lhys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhys.mapper.AssetCategoryMapper;
import com.lhys.pojo.AssetCategory;
import com.lhys.service.AssetCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 资产_类别表 服务实现类
 * </p>
 *
 * @author 小七
 * @since 2021-05-06
 */
@Service
public class AssetCategoryServiceImpl implements AssetCategoryService {
    @Autowired
    private AssetCategoryMapper categoryMapper;

    @Override
    @Transactional
    public Boolean saveAssetCategory(AssetCategory assetCategory) {
        try {
            if (assetCategory.getId() == null) {
                if (assetCategory.getCreateTime() == null) {
                    assetCategory.setCreateTime(new Date());
                }
                return categoryMapper.addAssetCategory(assetCategory) > 0;
            }
            return categoryMapper.updateAssetCategory(assetCategory) > 0;
        } catch (Exception e) {
            System.out.println("添加了重复元素");
            return null;
        }
    }

    @Override
    @Transactional
    public Boolean delAssetCategoryById(Integer id) {
        return categoryMapper.delAssetCategory(id) > 0;
    }

    @Override
    public AssetCategory getAssetCategoryById(Integer id) {
        return categoryMapper.getAssetCategoryById(id);
    }

    @Override
    public PageInfo<AssetCategory> getAssetCategoryList(AssetCategory assetCategory, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AssetCategory> list = categoryMapper.getAssetCategoryByCodeAndTitleOrRemarkList(assetCategory);
        PageInfo<AssetCategory> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


}
